package com.robot.backend.controller.robot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.robot.backend.service.robot.robotTestService;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Map;

@RestController
@RequestMapping("/robot/")
public class robotTestController {
    @Autowired
    robotTestService robotTestService;
    @RequestMapping("startTest/")
    public String startTest(@RequestParam MultiValueMap<String,String> map){
        System.out.println(map+"获取到");
        return null;
       // return robotTestService.startTest(Integer.parseInt(map.get("uid")),Integer.parseInt(map.get("bid")));
    };
    @RequestMapping("codeTest/")
    public String codeTest(@RequestParam Map<String,String> map){
        int[][] g =
                {
                        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
                        {1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1},
                        {1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1},
                        {1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1},
                        {1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                        {1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
                        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
                        {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1},
                        {1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1},
                        {1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1},
                        {1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1},
                        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
                };
        int[] steps = {1,2,3,1,};
        String stringg = Arrays.deepToString(g);//地图转字符串
        stringg = stringg.replaceAll("[\\s,\\[\\]]", "");
        String stringsteps = Arrays.toString(steps);
        stringsteps=stringsteps.replaceAll("[\\s,\\[\\]]", "");
        Process process = null;
        try {
            String[] cmd = { "sh", "/www/wwwroot/c/test.sh",stringg, "2","2","2","2",stringsteps, "2"};
            process = Runtime.getRuntime().exec(cmd);//执行调用
            int exitCode = process.waitFor();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream())
            );

            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println("[输出] " + line);
            }
            System.out.println("Exit Code: " + exitCode);
        } catch (IOException | InterruptedException e) {
            throw new RuntimeException(e);
        }

//        try {
//            String[] cmd = { "sh", "/www/wwwroot/cpp/test.sh",stringg,"myx","myy","oppox","oppoy",stringsteps,"2"};
//            process = Runtime.getRuntime().exec(cmd);//执行调用
//            int exitCode = process.waitFor();
//            BufferedReader reader = new BufferedReader(
//                    new InputStreamReader(process.getInputStream())
//            );
//
//            String line;
//            while ((line = reader.readLine()) != null) {
//                System.out.println("[输出] " + line);
//            }
//            System.out.println("Exit Code: " + exitCode);
//        } catch (IOException | InterruptedException e) {
//            throw new RuntimeException(e);
//        }
        return "success";
        //return robotTestService.writeTest(map.get("code"));
    }
}
